package com.pms.controller;

import com.pms.dto.CommunityPageQueryDTO;
import com.pms.dto.CommunitySaveDTO;
import com.pms.entity.Community;
import com.pms.result.PageResult;
import com.pms.result.Result;
import com.pms.service.CommunityService;
import com.pms.vo.CommunitySelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小区相关接口
 */
@Api(tags = "小区相关接口")
@RestController
@RequestMapping("/community")
@Slf4j
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    /**
     * 新增小区
     * @param communitySaveDTO
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增小区")
    public Result<String> save(@RequestBody CommunitySaveDTO communitySaveDTO) {
        log.info("新增小区:{}", communitySaveDTO);
        communityService.saveCommunity(communitySaveDTO);
        return Result.success();
    }

    /**
     * 小区分页查询
     * @param communityPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("小区分页查询")
    public Result<PageResult> page(CommunityPageQueryDTO communityPageQueryDTO){
        log.info("小区分页查询:{}",communityPageQueryDTO);
        PageResult pageResult=communityService.pageQuery(communityPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("根据id批量删除")
    public Result<String> delete(@RequestParam List<Integer> ids){
        log.info("根据id批量删除:{}",ids);
        communityService.delete(ids);
        return Result.success();
    }

    /**
     * 修改信息
     * @param community
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改信息")
    public Result<String> update(@RequestBody Community community){
        log.info("修改信息:{}",community);
        communityService.update(community);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<Community> getById(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        Community community=communityService.getById(id);
        return Result.success(community);
    }

    /**
     * 小区选择列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("小区选择列表")
    public Result<List<CommunitySelectVO>> list(){
        log.info("小区选择列表");
        List<CommunitySelectVO> voList=communityService.list();
        return Result.success(voList);
    }
}
